package com.edu.modules.baseinfo.service;

import com.alibaba.fastjson.JSONObject;
import com.edu.modules.baseinfo.dao.AccountDao;
import com.edu.modules.baseinfo.dao.StudentDao;
import com.edu.modules.baseinfo.vo.EvaluateListVo;
import com.edu.modules.baseinfo.vo.ResultVo;
import com.edu.modules.comm.Utils;
import com.edu.modules.comm.WXData;
import com.edu.modules.entity.TEvaluateEntity;
import com.edu.modules.entity.TLessonEvaluationEntity;
import com.edu.modules.entity.TLessonRecordEntity;
import com.sari.common.service.BaseService;
import com.sari.common.utils.DateUtils;
import com.sari.common.utils.IdGen;
import com.sari.common.utils.Reflections;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by kwj on 2016/7/15.
 */
@Service
public class StudentService extends BaseService {

	@Autowired
	private StudentDao studentDao;

	@Autowired
	private AccountDao accountDao;

	public JSONObject getStudentList(JSONObject param) {

		int offset = param.getInteger("offset");
		int limit = param.getInteger("limit");
		String name = param.getString("name");
		String grade = param.getString("grade");
		String lessonName = param.getString("lessonName");
		String startTime = param.getString("startTime");
		String endTime = param.getString("endTime");
		String phone = param.getString("phone");
		String linkman = param.getString("linkman");

		List list = new ArrayList();
		list = studentDao.getStudentList(name,grade,phone,linkman,offset,limit);

		JSONObject result= new JSONObject();
		result.put("total",studentDao.getStudentCounts(name,grade,phone,linkman));
		result.put("rows", list);

		return result;
	}

	public JSONObject getStudentCountStatistics(JSONObject param){

		String grade = param.getString("grade");
		String lessonName = param.getString("lesson");
        List counts = new ArrayList();
		List<Map> dateList = Utils.getDateList();
		for(Map m : dateList){

			counts.add(studentDao.getStudentCountStatistics(grade,lessonName,
					m.get("startDate").toString(),m.get("endDate").toString()));
		}
		JSONObject result= new JSONObject();
		result.put("rows", counts);
		return result;
	}

	public ResultVo saveEvaluate(JSONObject param){

		TEvaluateEntity evaluate = new TEvaluateEntity();
		param.put("evaluateId", IdGen.uuid());
		param.put("createTime", DateUtils.getDateTime());
		Reflections.initBean(evaluate,param);

		studentDao.saveObject(evaluate);
		return new ResultVo(true,"");
	}

	public ResultVo saveLessonEvaluate(JSONObject param){

		TLessonEvaluationEntity evaluate = new TLessonEvaluationEntity();
		param.put("evaluateId", IdGen.uuid());
		param.put("createTime", DateUtils.getDateTime());
		Reflections.initBean(evaluate,param);

		studentDao.saveObject(evaluate);
		return new ResultVo(true,"");
	}

	public EvaluateListVo getEvaluateList(String code){

		Map student = accountDao.getStudentId(WXData.getOpenId(code));
		String studentId = student.get("student_id").toString();//String.valueOf(accountDao.getStudentId(WXData.getOpenId(code)));
		if(studentId==null){
			return new EvaluateListVo(null,"未注册");
		}else{
			return new EvaluateListVo(studentDao.getEvaluateList(studentId),"success");
		}
	}

	public EvaluateListVo getTeacherEvaluateList(String studentId){

        return new EvaluateListVo(studentDao.getEvaluateList(studentId),"success");

	}

	public EvaluateListVo getLessonEvaluateListByCode(String code){

		Map student = accountDao.getStudentId(WXData.getOpenId(code));
		String studentId = student.get("student_id").toString();//String.valueOf(accountDao.getStudentId(WXData.getOpenId(code)));
		if(studentId==null){
			return new EvaluateListVo(null,"未注册");
		}else{
			return new EvaluateListVo(studentDao.getLessonEvaluateList(studentId),"success");
		}
	}

	public EvaluateListVo getLessonEvaluateList(String studentId){

	    return new EvaluateListVo(studentDao.getLessonEvaluateList(studentId),"success");

	}

	public JSONObject getLessonPlay(String studentId) {

		JSONObject result = new JSONObject();
		result.put("LessonPlay",studentDao.getLessonPlay(studentId));
		return result;
	}
}
